package com.hexiang.web;

import com.hexiang.domain.CommonResponse;
import com.hexiang.domain.User;
import com.hexiang.service.UserHttpService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class UserHttpClientController {

    @Resource
    private UserHttpService userHttpService;

    @GetMapping("/getUsers")
    public CommonResponse getUsers() {
        return userHttpService.getUsers();
    }
    @GetMapping("/getUsersTimeOut")
    public CommonResponse getUserTimeOut() {
        return userHttpService.getUsersTimeOut();
    }
    @GetMapping("/getUserErr")
    public CommonResponse getUserErr() {
        return userHttpService.getUsersErr();
    }

    @GetMapping("/getUserByName/{name}")
    public CommonResponse getUserByName(@PathVariable String name) {
        return userHttpService.getUserByName(name);
    }

    @GetMapping("/saveUser")
    public CommonResponse saveUser(@RequestParam String name, @RequestParam int age) {
        return userHttpService.addUser(name, age);
    }

    @GetMapping("/delUser")
    public CommonResponse delUser(@RequestParam Long id) {
        return userHttpService.deleteUserById(id);
    }

    @GetMapping("/updateUser")
    public CommonResponse updateUser(@RequestParam long id, @RequestParam String name, @RequestParam int age) {
        return userHttpService.updateUser(User.builder().id(id).name(name).age(age).build());
    }

    @GetMapping("/getPage")
    public CommonResponse getPage(@RequestParam int pageNum, @RequestParam int pageSize) {
        return userHttpService.getUserPage(pageNum, pageSize);
    }
}